package com.tsingteng.cosfun.utils;

import android.content.ContentValues;
import android.text.TextUtils;
import android.widget.SeekBar;

import com.google.gson.Gson;
import com.tsingteng.cosfun.app.AppContext;
import com.tsingteng.cosfun.bean.DBBean;
import com.tsingteng.cosfun.bean.DBPlayBean;
import com.tsingteng.cosfun.bean.DBVideo;
import com.tsingteng.cosfun.bean.JoinCostarBean;
import com.tsingteng.cosfun.bean.NotifictionNumerBean;
import com.tsingteng.cosfun.callback.OnDBSaveListener;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/30
 * qq(872179966)
 */

public class DBUtils {
    private static final String TAG ="DBUtils";

    private static List<DBVideo> list;

    /**
     * 查询便当
     *
     * @param name
     * @return
     */
    public static List<DBPlayBean> selectPlayDB(String name) {
        return DataSupport.where("name = ?", name).find(DBPlayBean.class);
    }


    /**
     * 删除便当
     *
     * @param content
     * @return
     */
    public static boolean deletePlayDB(String content) {
        return DataSupport.deleteAll(DBPlayBean.class, "path = ?", content) == 0 ? false : true;
    }


    /**
     * 根据type保存joinBean字符串
     * @param type
     * @param joinCostarBean
     * @return
     */
    public static void saveJoinCostarBean( String type,JoinCostarBean joinCostarBean) {
        ThreadPoolUtils.getmInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                Gson gson = new Gson();
                joinCostarBean.setVideoType(type);
                String joinBean = gson.toJson(joinCostarBean);
                DBBean dbBean = new DBBean();
                dbBean.setName(type);
                dbBean.setPath(joinBean);
                boolean save = dbBean.save();
                LogUtils.e(TAG,"保存成功与否" + save + "=========>" + type);
                ThreadPoolUtils.getmInstance().removeTask(this);
            }
        });

    }

    /**
     * 根据type更新视屏
     * @param type
     * @param joinCostarBean
     * @return
     */
    public static void UpDataJoinBean(String type, JoinCostarBean joinCostarBean, OnDBSaveListener onDBSaveListener) {
        ThreadPoolUtils.getmInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                ContentValues values = new ContentValues();
                Gson gson = new Gson();
                String s = gson.toJson(joinCostarBean);
                values.put("path",s);
                int i = DataSupport.updateAll(DBBean.class, values, "name = ?", type);
                LogUtils.e(TAG,"更新成功与否  i ===" + i + "type =========>" + type);
                UIUtils.runOnUIThread(() -> {
                    if (i>0) {
                        onDBSaveListener.onSaveSuccess();
                    }else {
                        onDBSaveListener.onSaveFail();
                    }
                });

                ThreadPoolUtils.getmInstance().removeTask(this);
            }
        });

    }

    /**
     * 根据名称删除对应的视屏缓存
     * @param type
     * @return
     */
    public static void deleteJoinBean(String type,OnDBSaveListener onDBSaveListener) {
        ThreadPoolUtils.getmInstance().executeTask(new Runnable() {
            @Override
            public void run() {
                int i = DataSupport.deleteAll(DBBean.class, "name = ? ", type);
                LogUtils.e(TAG,"删除成功与否  i ===" + i + "type =========>" + type);
                UIUtils.runOnUIThread(() -> {
                    if (i>0) {
                        onDBSaveListener.onSaveSuccess();
                    }else {
                        onDBSaveListener.onSaveFail();
                    }
                });
                ThreadPoolUtils.getmInstance().removeTask(this);
            }
        });

    }

    /**
     * 根据type查询JoinCostarBean
     * @param type
     * @return
     */
    public static JoinCostarBean getJoinCostarBean(String type) {
        List<DBBean> dbBeans = DataSupport.where("name = ?", type).find(DBBean.class);
        if (dbBeans!= null && dbBeans.size()<=0)  return null;
        DBBean dbBean = dbBeans.get(0);
        Gson gson = new Gson();
        JoinCostarBean joinCostarBean = gson.fromJson(dbBean.getPath(), JoinCostarBean.class);
        return joinCostarBean;
    }

    /**
     * 查询最后一个JoinCostarBean(个人中心显示)
     * @return
     */
    public static JoinCostarBean getLastJoinCostarBean() {
        List<DBBean> dbBeans = DataSupport.findAll(DBBean.class);
        if (dbBeans!= null && dbBeans.size()<=0)  return null;
        DBBean dbBean = dbBeans.get(dbBeans.size() - 1);
        Gson gson = new Gson();
        JoinCostarBean joinCostarBean = gson.fromJson(dbBean.getPath(), JoinCostarBean.class);
        return joinCostarBean;
    }

    /**
     * 得到全部的JoinCostarBean数据
     * @return
     */
   public static List<JoinCostarBean> getAllJoinCostarBean() {
       List<JoinCostarBean> listVideo = new ArrayList<>();
       List<DBBean> dbBeans = DataSupport.findAll(DBBean.class);
       if (dbBeans!= null && dbBeans.size()<=0)  return new ArrayList<>();
       Gson gson = new Gson();
       for (int i = 0; i < dbBeans.size(); i++) {
           JoinCostarBean joinCostarBean = gson.fromJson(dbBeans.get(i).getPath(), JoinCostarBean.class);
           listVideo.add(joinCostarBean);
       }

       return listVideo;
   }
}
